package com.meilai.project.controller.app.appController;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.app.*;
import com.meilai.project.dto.business.installOrder.WorkerSubmitChangePhotoDTO;
import com.meilai.project.dto.business.intentionScheme.BuildingInfo;
import com.meilai.project.dto.business.intentionScheme.PointProcessSearchDTO;
import com.meilai.project.service.app.AppWorkerService;
import com.meilai.project.service.business.installOrder.WorkOrderService;
import com.meilai.project.service.business.installOrder.WorkerSubmitService;
import com.meilai.project.service.business.intentionScheme.IntentionSchemeService;
import com.meilai.project.vo.app.WorkerIndexVO;
import com.meilai.project.vo.app.WorkerQueryWorkOrderProgressVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张驰
 * @date 2021/10/30 21:55
 */
@Api(value = "app工人控制器", tags = {"app工人"})
@RestController
@RequestMapping("api/app/worker")
public class AppWorkerController {

    @Autowired
    private AppWorkerService appWorkerService;
    @Autowired
    private WorkerSubmitService workerSubmitService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private IntentionSchemeService intentionSchemeService;

    @ApiOperation(value = "替换工单点位", notes = "替换工单点位")
    @ResponseBody
    @PostMapping("replaceWorkOrderPoint")
    public CommonResult replaceWorkOrderPoint(@RequestBody @Valid @ApiParam(name = "替换点位对象", value = "传入json格式", required = true) AppReplacePointDTO dto,
                                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(workOrderService.replacePoint(dto));
    }

    @ApiOperation(value = "获取工人首页信息", notes = "请求获取工人首页信息", response = WorkerIndexVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("get-index-info")
    public CommonResult getIndexInfo() {
        return new CommonResult().success(appWorkerService.getWorkerIndexInfo());
    }

    @ApiOperation(value = "工人提交安装信息", notes = "工人提交安装信息")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("submit-to-audit")
    public CommonResult submitToAudit(HttpServletRequest request,
                                      @RequestBody @Valid @ApiParam(name = "安装信息对象", value = "传入json格式", required = true) WorkerSubmitDeviceDTO dto,
                                      BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(workerSubmitService.addOneForApp(dto));
    }

    @ApiOperation(value = "工人提交维修申报", notes = "工人提交维修申报")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("submit-to-apply-repair")
    public CommonResult submitToApplyRepair(HttpServletRequest request,
                                            @RequestBody @Valid @ApiParam(name = "安装信息对象", value = "传入json格式", required = true) WorkerSubmitRepairDTO dto,
                                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(workerSubmitService.submitToApplyRepair(dto));
    }

    @ApiOperation(value = "查询工单列表", notes = "查询工单列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("get-work-order-list")
    public CommonResult getWorkOrderList(@RequestBody @Valid @ApiParam(name = "查询工单列表对象", value = "传入json格式", required = true) WorkerQueryWorkOrderDTO dto,
                                         BindingResult bindingResult) {
        return new CommonResult().success(appWorkerService.queryWorkOrderList(dto));
    }

    @ApiOperation(value = "查询工单进度", notes = "查询工单进度", response = WorkerQueryWorkOrderProgressVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("get-work-order-progress")
    public CommonResult getWorkOrderProgress(@RequestBody @Valid @ApiParam(name = "查询工单进度对象", value = "传入json格式", required = true) QueryWorkOrderProgressDTO dto,
                                             BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(appWorkerService.queryWorkOrderProgress(dto));
    }

    @GetMapping("/getOrderDetail")
    @ApiOperation("根据工单ID获取工单详情")
    public CommonResult getOrderDetail(@RequestParam(name = "orderId") Long orderId,
                                       @RequestParam(name = "orderType") Integer orderType,
                                       @RequestParam(name = "isSubmitted") Boolean isSubmitted,
                                       @RequestParam(value = "code", required = false) String code,
                                       @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                                       @RequestParam(value = "page") Integer page,
                                       @RequestParam(value = "page_size") Integer page_size) {
        return new CommonResult().success();
    }

    @ApiOperation(value = "查看工单列表", notes = "查看工单列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("/getWorkOrderListByApp")
    public CommonResult getWorkOrderListByApp(@RequestBody @Valid @ApiParam(name = "查询工单列表对象", value = "传入json格式", required = true) WorkerQueryWorkOrderDTO dto,
                                         BindingResult bindingResult) {
        return new CommonResult().success(appWorkerService.getWorkOrderListByApp(dto));
    }

    @GetMapping("/getOrderDetailByApp")
    @ApiOperation("根据工单ID获取工单详情")
    public CommonResult getOrderDetailByApp(@RequestParam(name = "orderId") Long orderId,
                                       @RequestParam(name = "orderType") Integer orderType,
                                       @RequestParam(name = "isSubmitted") Boolean isSubmitted,
                                       @RequestParam(value = "code", required = false) String code,
                                       @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                                       @RequestParam(value = "page") Integer page,
                                       @RequestParam(value = "page_size") Integer page_size) {
        if (orderType == 1 && !isSubmitted) {
            return new CommonResult().success(appWorkerService.getOrderNoInstallByApp(orderId, building_area_id));
        } else {
            return new CommonResult().success(appWorkerService.getOrderDetailByApp(orderId, isSubmitted, code, building_area_id, orderType, page, page_size));
        }
    }

    @GetMapping("/getOrderBuilding")
    @ApiOperation("根据工单ID和TYPE获取楼盘列表")
    public CommonResult getBuildingByOrderIdAndOrderType(@RequestParam(name = "orderId") Long orderId,
                                                         @RequestParam(name = "orderType") Integer orderType,
                                                         @RequestParam(value = "code", required = false) String code,
                                                         @RequestParam(value = "page") Integer page,
                                                         @RequestParam(value = "page_size") Integer page_size) {
        return new CommonResult().success(appWorkerService.getBuildingByOrderIdAndOrderType(orderId, orderType, code, page, page_size));
    }

    @GetMapping("/getBuildingList")
    @ApiOperation("查看楼盘列表")
    public CommonResult getBuildingList(@RequestParam(value = "code", required = false) String code,
                                        @RequestParam(value = "page") Integer page,
                                        @RequestParam(value = "page_size") Integer page_size) {
        return new CommonResult().success(appWorkerService.getBuildingList(code, page, page_size));
    }

    @GetMapping("/getBuildingDeviceList")
    @ApiOperation("根据楼盘id查看设备列表")
    public CommonResult getBuildingDeviceList(@RequestParam(value = "building_area_id") Long building_area_id,
                                              @RequestParam(value = "code", required = false) String code,
                                              @RequestParam(value = "page") Integer page,
                                              @RequestParam(value = "page_size") Integer page_size) {
        return new CommonResult().success(appWorkerService.getBuildingDeviceList(building_area_id, code, page, page_size));
    }

    @GetMapping("/getOrderByDevice")
    @ApiOperation("根据设备id查看工单列表")
    public CommonResult getOrderByDevice(@RequestParam(value = "device_id") Long device_id) {
        return new CommonResult().success(appWorkerService.getOrderByDevice(device_id));
    }

    @GetMapping("/getDeviceByDateRangeAndBuildingLocations")
    @ApiOperation("根据时间段、楼盘经纬度查看设备列表")
    public CommonResult getDeviceByDateRangeAndBuildingLocations(@RequestParam(value = "start_at") String start_at,
                                                                 @RequestParam(value = "end_at") String end_at,
                                                                 @RequestParam(value = "lat") String lat,
                                                                 @RequestParam(value = "lng") String lng,
                                                                 @RequestParam(value = "media_type_id") String media_type_id,
                                                                 @RequestParam(value = "building_area_id") String building_area_id) {
        // 时间段
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate beginDate = LocalDate.parse(start_at, df);
        LocalDate endDate = LocalDate.parse(end_at, df);
        // 楼盘经纬度
//        LocationInfo location = new LocationInfo();
//        location.setLat(Double.valueOf(lat));
//        location.setLng(Double.valueOf(lng));
//        List<LocationInfo> building_locations = new ArrayList<>();
//        building_locations.add(location);
        // 点位类型
        List<Long> point_type_ids = new ArrayList<>();
        point_type_ids.add(Long.valueOf(media_type_id));
        // 楼盘
        List<BuildingInfo> bis = new ArrayList<>();
        BuildingInfo bi = new BuildingInfo();
        bi.setId(Long.valueOf(building_area_id));
        bi.setLat(new BigDecimal(lat));
        bi.setLng(new BigDecimal(lng));
        bis.add(bi);
        // 封装数据
        PointProcessSearchDTO dto = new PointProcessSearchDTO();
        dto.setBeginDate(beginDate);
        dto.setEndDate(endDate);
//        dto.setPositionLocationList(building_locations);
        dto.setPointTypeIdList(point_type_ids);
//        dto.setBuildingRadius(5000);
        dto.setBuildingList(bis);
        dto.setBuildingRadiation(false);
        return new CommonResult().success(intentionSchemeService.search(dto));
    }

    @GetMapping("/getAuditInfo")
    @ApiOperation("根据提交记录id，查询信息")
    public CommonResult getAuditInfo(@RequestParam(value = "audit_id") Long audit_id) {
        return new CommonResult().success(workerSubmitService.getAuditForApp(audit_id));
    }

    @ApiOperation(value = "提交更改照片结果", notes = "提交更改照片结果")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("submit-change-photo")
    public CommonResult submitChangePhoto(@RequestBody @Valid @ApiParam(name = "更改提交照片结果", value = "传入json格式", required = true) WorkerSubmitChangePhotoDTO dto,
                                          BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(workerSubmitService.changePhotoForApp(dto));
    }
}
